/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forumgui;

import TamirsGui.GuiMessage;
import TamirsGui.GuiMsg;
import TamirsGui.MainWindowMsg;
import java.awt.JobAttributes;
import java.util.Vector;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Roy
 */
public class ReceiveMsgWorker extends SwingWorker<Vector<GuiMessage>, Void> {

    private String msg;
    private Forum f;
            

    public ReceiveMsgWorker(String msg,Forum f) {
        this.msg = msg;
        this.f = f;
    }
    
    

    @Override
    public Vector<GuiMessage> doInBackground() throws Exception
    {
        MainWindowMsg parser = new MainWindowMsg();
        Vector<GuiMessage> parseMessage = parser.parseMessage(msg);
        System.out.println(parseMessage);
        
        return parseMessage;
    }
    
    @Override
    public void done()
    {
         Vector<GuiMessage> msgs  = null;
         
        try {
             msgs = get();
        } catch (InterruptedException ex) {
            //TODO logger msg
        } catch (ExecutionException ex) {
            
        }
        if (msgs.size() > 0)
        {
            GuiMessage firstMsg = msgs.firstElement();
            switch (firstMsg.getType())
            {
                case ack:
                    f.showAckMsg(firstMsg);//can there be more than one ?
                    switch (firstMsg.getSubType()) 
                    {
                        case subForumAdded:
                            f.showCandidateList(msgs,GuiMessage.MessageSubType.candidateList);
                            break;
                        case pathNotFound:
                            f.doBack();
                    }
                    break;
                case content:
                    checkWhatIsTheSubType(msgs,firstMsg);
                    break;
                case popup:
                    f.refreshPopupMenu(msgs);
                    break;
                case refresh:
                        f.refreshMainWindow();
                    break;
            }
        }
        else
        {
            System.out.println("empty msgs");
        }
        
        
    }

    private void checkWhatIsTheSubType(Vector<GuiMessage> msgs, GuiMessage firstMsg) {
        switch (firstMsg.getSubType())
        {
            case threadMsg:case subForum:case postMsg:
                f.updateMainWindow(msgs);
                break;
            case moderatorList: case candidateList: case usersList:case adminList:
                f.showCandidateList(msgs,firstMsg.getSubType());
                break;
        }
        
    }
    
}
